﻿
using Microsoft.EntityFrameworkCore;
using FreeRedis;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Mvc;
using static System.Reflection.Metadata.BlobBuilder;
using Newtonsoft.Json.Linq;
using CQIE.LabAssist.Models;
using System;
using CQIE.LabAssist.PublicHelp;
using System.Drawing;
using System.Data;
namespace CQIE.LabAssist.DataAccesss
{

    public class CheckService : ICheckService
    {


        private readonly _DbContext Db;
        public CheckService(JwtHelp jwtHelper, _DbContext db)
        {

            Db = db;
        }

        /// <summary>
        /// 新增检查表
        /// </summary>
        /// <param name="check"></param>
        /// <returns></returns>
        public bool addCheck(Check check)
        {
            using (_DbContext _Db = new _DbContext())
            {
                Check newcheck = new Check
                {
                    DateTime = DateTime.Today.Date.AddHours(0),
                    Anti_theft_facilities = check.Anti_theft_facilities,
                    Anti_theft_facilities_state = check.Anti_theft_facilities_state,
                    Doors_windows = check.Doors_windows,
                    Doors_windows_state = check.Doors_windows_state,
                    Electrical_wiring = check.Electrical_wiring,
                    Electrical_wiring_state = check.Electrical_wiring_state,
                    Firefighting_facilities = check.Firefighting_facilities,
                    Firefighting_facilities_state = check.Firefighting_facilities_state,
                    Firefighting_facilities_validity = check.Firefighting_facilities_validity,
                    Firefighting_facilities_validity_state = check.Firefighting_facilities_validity_state,
                    Instruments_equipment = check.Instruments_equipment,
                    Instruments_equipment_state = check.Instruments_equipment_state,
                    Warning_sign = check.Warning_sign,
                    Warning_sign_state = check.Warning_sign_state,
                    IEOther_safety_hazards = check.IEOther_safety_hazards,
                    IEOther_safety_hazards_state = check.IEOther_safety_hazards_state,
                    Item_hygiene = check.Item_hygiene,
                    Item_hygiene_state = check.Item_hygiene_state,
                    Other_safety_hazards = check.Other_safety_hazards,
                    Other_safety_hazards_state = check.Other_safety_hazards_state,
                    laboratoryId = check.laboratoryId,
                    semesterId = check.semesterId
                };
                if (check.Anti_theft_facilities && check.Doors_windows &&
                   check.Electrical_wiring && check.Electrical_wiring &&
                   check.Firefighting_facilities && check.Firefighting_facilities_validity &&
                   check.Instruments_equipment && check.Warning_sign && check.IEOther_safety_hazards &&
                   check.Item_hygiene && check.Other_safety_hazards)
                {
                    newcheck.State = true;
                }
                else
                {
                    newcheck.State = false;
                }
                newcheck.semester = _Db.Semesters.Where(w => w.Id == check.semesterId).FirstOrDefault();
                newcheck.laboratory = _Db.Laboratories.Where(W => W.Id == check.laboratoryId).FirstOrDefault();
                _Db.Checks.Add(newcheck);

                return _Db.SaveChanges() > 0;
            }
        }

       
    }


}
